<?php
namespace app\admin\controller;
use app\admin\common\Base;
use think\Controller;
class Backup extends Base
{
    //数据库备份
    public function index()
    {      
            //获取操作内容：（备份/下载/还原/删除）数据库
            $data=$this->request->param();
            //$type= isset($data['type'])?$data['type']:'';
            $type=input('type');
            //获取需要操作的数据库名字
            $name=input('name');
            //$name=isset($data['name'])?$data['name']:'';
            $backup = new \org\Baksql(\think\Config::get("database"));
            switch ($type) {
            //备份
            case "backup":
                $info = $backup->backup();
                //return ['status'=>1,'msg'=>'aaa'];
                // halt($info);
                $this->success("$info", 'admin/backup/index');
                // break;
            //下载
            case "dowonload":
                $info = $backup->downloadFile($name);
                $this->success("$info", 'admin/backup/index');
                break;
            //还原
            case "restore":
                $info = $backup->restore($name);
                return ['code'=>1,'info'=>$info];
                //$this->success("$info", 'admin/backup/index');
                break;
            //删除
            case "del":
                $info = $backup->delfilename($name);
                $this->success("$info", 'admin/backup/index');
                break;
          
            //如果没有操作，则查询已备份的所有数据库信息
            default:
                if(isset($data['ajax'])&&$data['ajax']==1){
                    $page = isset($data['page']) ? $data['page'] : 1;
                    $page = intval($page);
                    $limit = isset($data['rows']) ? $data['rows'] : 15;
                    $limit = intval($limit);
                    $start = $limit * ($page - 1);
    
                    $list=array_reverse($backup->get_filelist());//将信息由新到老排序
                    $count=count($list);
                    $alist=array_slice($list,$start,$limit,false);
                    //halt($list);
                    return $this->json_data($count, $alist);
                    break;

                }
                return $this->fetch();
               // return $this->json_data($count, $list);
               //return $this->fetch("index", ['rows'=>$count,"list" => $list]);//将信息由新到老排序
            }
          
      
    }
}
